package com.guit.hrms.mapper;

import com.guit.hrms.po.Article;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 文章数据访问层
 */
@Mapper
public interface ArticleMapper {
    /**
     * 查询所有启用的文章
     */
    @Select("SELECT * FROM articles WHERE status = 1 ORDER BY publish_date DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "title", column = "title"),
        @Result(property = "content", column = "content"),
        @Result(property = "publishDate", column = "publish_date"),
        @Result(property = "author", column = "author"),
        @Result(property = "editor", column = "editor"),
        @Result(property = "viewCount", column = "view_count"),
        @Result(property = "articleType", column = "article_type"),
        @Result(property = "hasAttachment", column = "has_attachment"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    List<Article> findAll();

    /*
    * 管理员查询所有文章(包括状态为禁用的文章)
    *
    */
    @Select("SELECT * FROM articles WHERE status = 0 OR status = 1 ORDER BY  publish_date DESC ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "author", column = "author"),
            @Result(property = "editor", column = "editor"),
            @Result(property = "viewCount", column = "view_count"),
            @Result(property = "articleType", column = "article_type"),
            @Result(property = "hasAttachment", column = "has_attachment"),
            @Result(property = "status", column = "status"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    List<Article> findAllWithToken();

    /**
     * 根据ID查询文章
     */
    @Select("SELECT * FROM articles WHERE id = #{id} AND status = 1")
    Article findById(Long id);
    
    /**
     * 根据文章类型查询
     */
    @Select("SELECT * FROM articles WHERE article_type = #{type} AND status = 1 ORDER BY publish_date DESC")
    List<Article> findByType(Integer type);
    
    /**
     * 新增文章
     */
    @Insert("INSERT INTO articles (title, content, publish_date, author, editor, " +
            "article_type, has_attachment, view_count, status, created_at, updated_at) " +
            "VALUES (#{title}, #{content}, #{publishDate}, #{author}, #{editor}, " +
            "#{articleType}, #{hasAttachment}, #{viewCount}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Article article);
    
    /**
     * 更新文章
     */
    @Update("UPDATE articles SET title = #{title}, content = #{content}, editor = #{editor}, " +
            "article_type = #{articleType}, has_attachment = #{hasAttachment}, updated_at = NOW() " +
            "WHERE id = #{id}")
    int update(Article article);
    
    /**
     * 删除文章（软删除）
     */
    @Update("UPDATE articles SET status = 0 WHERE id = #{id}")
    int delete(Long id);

    /**
     * 恢复文章
     */
    @Update("UPDATE articles SET status = 1 WHERE id = #{id}")
    int updateStatus(Long id);

    /**
     * 增加浏览次数
     */
    @Update("UPDATE articles SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(Long id);
    
    /**
     * 统计文章数量
     */
    @Select("SELECT COUNT(*) FROM articles WHERE status = 1")
    int countArticles();
    
    /**
     * 硬删除文章
     * @param id 文章ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM articles WHERE id = #{id}")
    int hardDelete(Long id);
    
    /**
     * 删除文章关联的所有附件
     * @param articleId 文章ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM attachments WHERE article_id = #{articleId}")
    int deleteAttachments(Long articleId);
} 